package cn.zh.stan.fragment;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.github.shenyuanqing.zxingsimplify.zxing.Activity.CaptureActivity;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.youth.banner.Banner;
import com.youth.banner.transformer.AccordionTransformer;
import com.youth.banner.transformer.CubeOutTransformer;
import com.youth.banner.transformer.ScaleInOutTransformer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.ITem.HgItem;
import cn.zh.stan.R;
import cn.zh.stan.activity.GoodsActivity;
import cn.zh.stan.adapter.HGadapter;
import cn.zh.stan.util.GlideImageLoader;
import cn.zh.stan.util.MyGridView;
import okhttp3.Request;
import okhttp3.Response;

import static android.app.Activity.RESULT_OK;

/**
 * Created by WYN on 2018/1/15 0015.
 */

public class HomeFragment extends Fragment implements View.OnClickListener{
    Banner banner;
    ImageView gdimage;
    ArrayList<String> imgs=new ArrayList<String>();
    MyGridView hgrid;
    HGadapter hGadapter;
    ArrayList<HgItem> hgItems=new ArrayList<HgItem>();
    TextView hot_sale,new_product,recommend,hfall;
    ImageView scimag;
    private static final int REQUEST_SCAN = 0;
    String keyword="";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View view =inflater.inflate(R.layout.fragment_home,container,false);
        banner=(Banner) view.findViewById(R.id.banner);
        gdimage=view.findViewById(R.id.gdimage);
        hgrid=view.findViewById(R.id.hgrid);
        initf(view);
        getIndeGet();
        return view;
         }


    public void initf(View view){
        hGadapter=new HGadapter(getActivity(),hgItems);
        hgrid.setAdapter(hGadapter);
        hot_sale=view.findViewById(R.id.hot_sale);
        new_product=view.findViewById(R.id.new_product);
        recommend=view.findViewById(R.id.recommend);
        hfall=view.findViewById(R.id.hfall);
        scimag=view.findViewById(R.id.scimag);
        hot_sale.setOnClickListener(this);
        new_product.setOnClickListener(this);
        recommend.setOnClickListener(this);
        hfall.setOnClickListener(this);
        scimag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getRuntimeRight();  
                
            }
        });
        }


    @Override
    public void onClick(View view) {
        int id=view.getId();
        Intent intent=null;
        switch (id){
            case R.id.hot_sale:
                intent=new Intent(getActivity(), GoodsActivity.class);
                break;
            case R.id.new_product:
                intent=new Intent(getActivity(), GoodsActivity.class);
                break;
            case R.id.recommend:
                intent=new Intent(getActivity(), GoodsActivity.class);
                break;
            case R.id.hfall:
                intent=new Intent(getActivity(), GoodsActivity.class);
                break;
          }
        startActivity(intent);
        }


    /**
     * 获得运行时权限
     */
    private void getRuntimeRight() {
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, 1);
        } else {
            jumpScanPage();
          }
      }


    /**
     * 跳转到扫码页
     */
    private void jumpScanPage() {
        startActivityForResult(new Intent(getActivity(), CaptureActivity.class),REQUEST_SCAN);
        }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data==null){
            return;
        }
        if(requestCode == REQUEST_SCAN && resultCode == RESULT_OK){
            keyword=data.getStringExtra("barCode");
        }
       }



    //获取首页数据
    public void getIndeGet(){
        OkHttpUtils.get(BaseUrl.index_Get)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                              boolean Flag=js.getBoolean("Flag");
                              if (Flag){
                                  JSONArray ja=js.getJSONArray("CX");
                                  hgItems.clear();
                                  for (int i=0;i<ja.length();i++){
                                  JSONObject jo=ja.getJSONObject(i);
                                  String GoodsId=jo.getString("GoodsID");
                                  String GoodsNo=jo.getString("GoodsNo");
                                  String ClassId=jo.getString("ClassId");
                                  String GoodsName=jo.getString("GoodsName");
                                  String GoodsSpec=jo.getString("GoodsSpec");
                                  String Unit=jo.getString("Unit");
                                  String Price_Cost=jo.getString("Price_Cost");
                                  String Price_Detail=jo.getString("Price_Detail");
                           HgItem hgItem=new HgItem(GoodsId,GoodsNo,ClassId,GoodsName,GoodsSpec,Unit,Price_Cost,Price_Detail);
                                 hgItems.add(hgItem);
                                  }
                                 hGadapter.notifyDataSetChanged();

                              JSONArray Banner=js.getJSONArray("Banner");
                                  imgs.clear();
                             for (int i=0;i<Banner.length();i++){
                                 JSONObject jo=Banner.getJSONObject(i);
                                 String Pic=jo.getString("Pic");
//                                 String url="http://120.26.41.167:8090"+Pic;
                                 imgs.add(Pic);
                                  }

                                  if (Banner.length()==0){
                                      gdimage.setVisibility(View.VISIBLE);
                                      banner.setVisibility(View.GONE);
                                  }else {
                                      gdimage.setVisibility(View.GONE);
                                      banner.setVisibility(View.VISIBLE);
                                      banner.setImages(imgs).setImageLoader(new GlideImageLoader())
                                              .setDelayTime(3000)
                                              .setBannerAnimation(ScaleInOutTransformer.class)
                                              .start();
                                  }
                              }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
        
        }


}


